/**
 * Copyright � 2012 GestureSoft. ALL RIGHTS RESERVED.
 * This file contains proprietary and GestureSoft CONFIDENTIAL Information.
 * Use, disclosure or reproduction is prohibited.
 * 
 * @fileName WalletInitializer.as
 * @creationDate No14 8, 2012
 * @author 
 * @version 1.0
 */
package com.gesturesoft.imultimax.model.cardmanager
{
	import com.gesturesoft.imultimax.model.DataBaseManager;
	import com.gesturesoft.imultimax.model.error.DataBaseError;
	import com.gesturesoft.imultimax.model.interfaces.IDatabaseManager;
	import com.gesturesoft.imultimax.model.vo.ElementMasterVO;
	import com.gesturesoft.imultimax.model.vo.MainCategoryMasterVO;
	import com.gesturesoft.imultimax.model.vo.PageVO;
	import com.gesturesoft.imultimax.model.vo.SubCategoryMasterVO;
	import com.gesturesoft.imultimax.model.vo.TemplateMasterVO;
	import com.gesturesoft.imultimax.model.vo.WalletMasterVO;
	
	import mx.collections.ArrayCollection;

	public class WalletInitializer
	{
		
		private var _arrMainCategories:ArrayCollection;
		
		public function WalletInitializer()
		{
			setMainCategoryVO();
			
		}
		
		
		private function createSubCategories(arrSubCategoryNames:Array):ArrayCollection
		{
			
			var element:ElementMasterVO = null;
			var subCategoryVO:SubCategoryMasterVO;
			var template:TemplateMasterVO = null;
			var page:PageVO = null;
			var arrSubCategories:ArrayCollection = new ArrayCollection();
			
			for(var i:int=0;i<arrSubCategoryNames.length;i++)
			{
				
				//===================================
				subCategoryVO = new SubCategoryMasterVO();
				subCategoryVO.name = arrSubCategoryNames[i];
				
				subCategoryVO.templates = new ArrayCollection();
				
				template = new TemplateMasterVO();
				template.sequence = 1;
				page = new PageVO();
				page.width = 300;
				page.height = 150;
				page.cornerRadius = 7;
				page.backgroundImage = "assets/images/card4.png";
				page.logo = "assets/images/card4/skin1.png";
				page.needRibon = false;
				
				page.element = new ArrayCollection();
				
				element = new ElementMasterVO();
				element.type = "spark.components.Label";
				element.changeType = "spark.components.TextInput";
				element.left = 35;
				element.right = 35;
				element.verticalCenter = 15;
				element.horizontalCenter = 0;
				element.needFormat = true;
				element.text = "0000000000000000";
				element.fontSize = 18; 
				element.fontWeight = "bold";
				element.color = 0xFFFFFF;
				element.textAlign = "justify";
				page.element.addItem(element);
				
				element = new ElementMasterVO();
				element.type = "views.ExpiryDateViewer";
				element.changeType = "views.CardExpiry";
				element.bottom = 25;
				element.horizontalCenter = 0;
				element.needFormat = false;
				element.label = "MM/YY";
				page.element.addItem(element);
				
				element = new ElementMasterVO();
				element.type = "spark.components.Label";
				element.changeType = "spark.components.TextInput";
				element.left = 35;
				element.right = 35;
				element.bottom = 8;
				element.needFormat = false;
				element.text = "Name";
				element.fontSize = 14; 
				element.fontWeight = "bold";
				element.color = 0xFFFFFF;
				page.element.addItem(element);
				
				template.page = page;
				subCategoryVO.templates.addItem(template);
				
				template = new TemplateMasterVO();
				template.sequence = 2;
				page = new PageVO();
				page.width = 300;
				page.height = 150;
				page.cornerRadius = 7;
				page.backgroundImage = null;
				page.logo = null;
				page.needRibon = true;
				page.element = new ArrayCollection();
				
				element = new ElementMasterVO();
				element.type = "spark.components.Label";
				element.changeType = "spark.components.TextInput";
				element.top = 45;
				element.right = 20;
				element.width = 50;
				element.fontSize = 18;
				element.fontWeight = "bold";
				element.color = 0x484545;
				element.textAlign = "center";
				element.needFormat = false;
				element.text = "0000";
				page.element.addItem(element);
				
				template.page = page;
				subCategoryVO.templates.addItem(template);
				
				arrSubCategories.addItem(subCategoryVO);
				
			}
			
			return arrSubCategories;
			
		}
		
		private function setMainCategoryVO():void
		{
			
			var mainCategoryMasterVO:MainCategoryMasterVO = null;
			var arrSubCategories:ArrayCollection = null;
			_arrMainCategories = new ArrayCollection();
			var element:ElementMasterVO = null;
			var subCategoryVO:SubCategoryMasterVO;
			var template:TemplateMasterVO = null;
			var page:PageVO = null;
			
			mainCategoryMasterVO = new MainCategoryMasterVO();
			mainCategoryMasterVO.name = "Payment Cards";
			mainCategoryMasterVO.type = null;
			mainCategoryMasterVO.subCategories = createSubCategories(["Visa", "MasterCard", "Amex", "JCB", "China Union", "Store Card"]);
			_arrMainCategories.addItem(mainCategoryMasterVO);
			
			mainCategoryMasterVO = new MainCategoryMasterVO();
			mainCategoryMasterVO.name = "Identity Documents";
			mainCategoryMasterVO.type = null;
			mainCategoryMasterVO.subCategories = createSubCategories(["ID"]);
			_arrMainCategories.addItem(mainCategoryMasterVO);
			
			mainCategoryMasterVO = new MainCategoryMasterVO();
			mainCategoryMasterVO.name = "Transaction Info";
			mainCategoryMasterVO.type = null;
			mainCategoryMasterVO.subCategories = createSubCategories(["Reciepts","Travel Tickets"]);
			_arrMainCategories.addItem(mainCategoryMasterVO);
			
			mainCategoryMasterVO = new MainCategoryMasterVO();
			mainCategoryMasterVO.name = "Stored Value";
			mainCategoryMasterVO.type = null;
			mainCategoryMasterVO.subCategories = createSubCategories(["Cash","Loyalty Cards"]);
			_arrMainCategories.addItem(mainCategoryMasterVO);
			
			mainCategoryMasterVO = new MainCategoryMasterVO();
			mainCategoryMasterVO.name = "Others";
			mainCategoryMasterVO.type = null;
			mainCategoryMasterVO.subCategories = createSubCategories(["Shopping Lists"]);
			_arrMainCategories.addItem(mainCategoryMasterVO);
			
		}
		
		public function initialize():void
		{
			
			var iDBManager:IDatabaseManager = new DataBaseManager();
			iDBManager.setTransactionStartPoint();
			
			try
			{
				
				insertMainCategories(iDBManager);
				
			} 
			catch(error:Error) 
			{
				trace(error);
			}
			
		}
		
		/**
		 * This is the function for inserting main categories like payment cards etc..
		 * 
		 * @functionName insertMainCategories
		 * @creationDate Sep 5, 2012
		 * @author GestureSoft
		 * @params query:String
		 */
		private function insertMainCategories(iDBManager:IDatabaseManager):void
		{
			
			for each(var item:MainCategoryMasterVO in this._arrMainCategories)
			{
				
				var query:String = "INSERT INTO ACTIVE_THEME ( cat_master_name, cat_master_type ) " + 
					"VALUES('"+ 
					item.name +"','" + 
					item.type +"')";
				
				executeHandler(query,iDBManager);
				
				var mainCategoryID:int = int( retrieveHandler("SELECT last_insert_rowid() as ID", iDBManager)[0].ID );
				
				for each(var subCategory in item.subCategories)
				{
					
					insertSubCategory( subCategory, mainCategoryID, iDBManager);
					
				}
				
				
			}
			
			
		}
		
		/**
		 * This is the function for inserting sub categories like payment cards etc..
		 * 
		 * @functionName insertSubCategory
		 * @creationDate Nov 15, 2012
		 * @author GestureSoft
		 * @params query:String
		 */
		private function insertSubCategory(subCategory:SubCategoryMasterVO, mainCategoryID:int, iDBManager:IDatabaseManager):void
		{
			
			var query:String;
			
			for each(var subCategoryMasterVO:SubCategoryMasterVO in subCategory)
			{
				query = "INSERT INTO IM_SUB_CAT_MASTER ( sub_cat_name, main_cat_id ) " + 
					"VALUES('"+ 
					subCategoryMasterVO.name +"'," + 
					mainCategoryID +")";
				
				executeHandler(query, iDBManager);
				
				var subCategoryID:int = int( retrieveHandler("SELECT last_insert_rowid() as ID", iDBManager)[0].ID );
				insertTemplate( subCategory.templates, subCategoryID, iDBManager );
				insertWallet( subCategoryID, iDBManager );
				
			}
			
		}
		
		/**
		 * This is the function for inserting insertWallet curresponding to a sub category
		 * 
		 * @functionName insertTemplate
		 * @creationDate Nov 15, 2012
		 * @author GestureSoft
		 * @params subCategoryID:int
		 * @params iDBManager:IDatabaseManager
		 */
		private function insertWallet( subCategoryID:int, iDBManager:IDatabaseManager ):void
		{
			var query:String;
			
			query = "INSERT INTO IM_WALLET_MASTER ( sub_cat_id ) VALUES("+subCategoryID +")";
			executeHandler(query, iDBManager);
			
		}
		
		/**
		 * This is the function for inserting template curresponding to a sub category
		 * 
		 * @functionName insertTemplate
		 * @creationDate Nov 15, 2012
		 * @author GestureSoft
		 * @params subCategoryID:int
		 * @params iDBManager:IDatabaseManager
		 */
		private function insertTemplate( templates:ArrayCollection, subCategoryID:int, iDBManager:IDatabaseManager ):void
		{
			
			var query:String;
			
			for each(var templateVO:TemplateMasterVO in templates)
			{
				
				query = "INSERT INTO IM_SUB_CAT_MASTER ( sub_cat_id, tmpl_sequence ) VALUES("+subCategoryID +","+1+")";
				executeHandler(query, iDBManager);
				var templateID:int = int( retrieveHandler("SELECT last_insert_rowid() as ID", iDBManager)[0].ID );
				
				insertPage( templateVO.page, templateID, iDBManager );
				
			}
			
		}
		
		/**
		 * This is the function for inserting page curresponding to a template
		 * 
		 * @functionName insertPage
		 * @creationDate Nov 15, 2012
		 * @author GestureSoft
		 * @params subCategoryID:int
		 * @params iDBManager:IDatabaseManager
		 */
		private function insertPage(pageVO:PageVO, templateID:int, iDBManager):void
		{
			var query:String;
			
			query = "INSERT INTO IM_SUB_CAT_MASTER ( page_sequence, page_width, page_height, page_corner_radius, page_bg_image, page_logo_image" +
				"page_need_ribon, page_tmpl_master_id ) " + 
				"VALUES('"+ 
				pageVO.sequence +"'," + 
				pageVO.width +"," + 
				pageVO.height +"," + 
				pageVO.cornerRadius +",'" + 
				pageVO.backgroundImage +"','" + 
				pageVO.logo +"'," + 
				pageVO.needRibon +"," + 
				templateID +")";
			executeHandler(query, iDBManager);
			
			var pageID:int = int( retrieveHandler("SELECT last_insert_rowid() as ID", iDBManager)[0].ID );
			for each(var element:ElementMasterVO in pageVO.element)
			{
				query = "INSERT INTO IM_SUB_CAT_MASTER ( element_type, element_change_type, element_width, element_height, element_left, element_right," +
					"element_top, element_bottom, element_horizontal_center,element_vertical_center, element_text,element_label,element_font_size," +
					"element_font_weight,element_color, element_text_align,element_need_format,element_page_id ) " + 
					"VALUES('"+ 
					element.sequence +"'," + 
					element.width +"," + 
					element.height +"," + 
					element.left +"," + 
					element.right +"," + 
					element.top +"," + 
					element.bottom +"," + 
					element.horizontalCenter +"," + 
					element.verticalCenter +",'" + 
					element.text +"','" + 
					element.label +"'," + 
					element.fontSize +",'" + 
					element.fontWeight +"','" + 
					element.color +"','" + 
					element.textAlign +"'," + 
					element.needFormat +"," + 
					pageID +")";
				
				executeHandler(query, iDBManager);
			}
			
			
		}
		
		//---------------------------------------------------------------DB Operators---------------------------------------------------------------//
		private function executeHandler(val:String, iDataBaseManger:IDatabaseManager):void
		{
			
			try
			{
				
				iDataBaseManger.execute(val);
				
			} 
			catch(error:DataBaseError) 
			{
				
				iDataBaseManger.rollBackTransactions();
				trace("ApplicationStyleUpdator-executeHandler() : Database execution error");
				throw error;
				
			}
			
		}
		
		
		/**
		 * This is the function for retrieving datas from database as collection of arrays
		 * 
		 * @functionName retreiveHandler
		 * @creationDate Sep 5, 2012
		 * @author GestureSoft
		 * @params query:String
		 */
		private function retrieveHandler(val:String, iDataBaseManager:IDatabaseManager=null):ArrayCollection
		{
			
			
			var _iDataBaseManager:IDatabaseManager;
			if(iDataBaseManager)
				_iDataBaseManager = iDataBaseManager;
			else
				_iDataBaseManager = new DataBaseManager;
			var result:ArrayCollection;
			
			try
			{
				
				result =  _iDataBaseManager.getData(val);
				
			} 
			catch(error:DataBaseError) 
			{
				
				trace("ApplicationStyleUpdator-retrieveHandler() : Database Read error");
				throw error;
				
			}
			
			return result;
			
		}
		
	}
}